﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EnglishAuction.BUS
{    
    public class NguoiThangCuocBUS
    {
        //private static EnglishAuctionEntities db = new EnglishAuctionEntities();

        public static bool UpdateRating(int id, int rating)
        {
            bool result = true;
            using (var context = new EnglishAuctionEntities())
            {
                NguoiThangCuoc ntc = context.NguoiThangCuocs.Where(p => p.Id == id).FirstOrDefault();
                ntc.Rating = rating;
                context.SaveChanges();
            }

            return result;
        }

        public static WinTemp GetNguoiThangCuocBySanPhamId(int id)
        {
            WinTemp winTemp = null;
            using (var context = new EnglishAuctionEntities())
            {
                var ntc = context.NguoiThangCuocs.Where(n => n.SanPhamId == id).FirstOrDefault();
                winTemp = new WinTemp();
                winTemp.Id = ntc.Id;
                winTemp.Price = ntc.Gia.Value;
                winTemp.UserId = ntc.NguoiDungId.Value;
                winTemp.Username = ntc.NguoiDung.TaiKhoan;
                if (ntc.ThanhToan != null && ntc.ThanhToan == 1)
                    winTemp.Payment = "Đã thanh toán";
                else
                    winTemp.Payment = "Chưa thanh toán";
            }

            return winTemp;
        }

        public static bool Update(int id)
        {
            bool result = true;
            using (var context = new EnglishAuctionEntities())
            {
                NguoiThangCuoc ntc = context.NguoiThangCuocs.Where(n => n.Id == id).FirstOrDefault();
                ntc.ThanhToan = 1;
                context.SaveChanges();
            }
            return result;
        }

        public static NguoiThangCuoc GetById(int id)
        {
            using (var context = new EnglishAuctionEntities())
            {
                return context.NguoiThangCuocs.Where(n => n.Id == id).FirstOrDefault();
            }
        }

        public static List<NguoiThangCuoc> GetListNguoiThangCuoc(int userId)
        {
            //db = new EnglishAuctionEntities();
            List<NguoiThangCuoc> lst = new List<NguoiThangCuoc>();
            using (var context = new EnglishAuctionEntities())
            {
                var tmp = context.NguoiThangCuocs.Where(n => n.NguoiDungId == userId);
                foreach (var item in tmp)
                {
                    if (item.SanPham.NgayLenSan.Value.AddDays(item.SanPham.ThoiGianDauGia.Value).CompareTo(DateTime.Now) < 0)
                    {
                        lst.Add(item);
                    }
                }
            }

            return lst;
          
        }

        public static bool Delete(int id)
        {
            bool result = true;
            using (var context = new EnglishAuctionEntities())
            {
                NguoiThangCuoc nguoiThangCuoc = context.NguoiThangCuocs.Where(p => p.Id == id).FirstOrDefault();
                context.NguoiThangCuocs.Remove(nguoiThangCuoc);
                context.SaveChanges();
            }

            return result;
        }

        public static NguoiThangCuoc CheckExist(int sanPhamId)
        {
            using (var context = new EnglishAuctionEntities())
            {
                return context.NguoiThangCuocs.Where(n => n.SanPhamId == sanPhamId).FirstOrDefault();
            }

        }

        public static bool Insert(NguoiThangCuoc nguoiThangCuoc)
        {
            bool result = true;
            using (var context = new EnglishAuctionEntities())
            {
                context.NguoiThangCuocs.Add(nguoiThangCuoc);
                context.SaveChanges();
            }

            return result;
        }

       
    }
}